package
{
	import flash.utils.Dictionary;
	
	import org.flixel.FlxG;
	import org.flixel.FlxGroup;
	import org.flixel.FlxSprite;
	import org.flixel.FlxText;
	
	public class TutorialAnimations extends FlxGroup
	{
		
		private static const NONE:int = 0;
		private static const YOU:int = 1;
		private static const YOURTASK:int = 2;
		private static const YOURTACTICS:int = 3;
		private static const YOURTOOLS:int = 4;
		private static const YOURTRANSFORM:int = 5;
		private static const YOURCOMBO:int = 6;
		private static const YOURCHICKENS:int = 7;
		
		private var backdrop:FlxSprite;
		private var theCurrentAnimation:FlxSprite;
		private var level:int;
		
		private var mouseClicked:Boolean;
		private var isOver:Boolean;
		
		private var tutorialState:int;
		
		public function TutorialAnimations(l:int)
		{
			isOver = false;
			level = l;
			backdrop = new FlxSprite(0, 0, Res.Backdrop);
			theCurrentAnimation = new FlxSprite(97,67, Res.blankImage);
			theCurrentAnimation.addAnimation("silent", [0], 1);
			theCurrentAnimation.addAnimation("you", [0], 1);
			theCurrentAnimation.addAnimation("yourTask", [0,1,2,3,4,5], 2);
			theCurrentAnimation.addAnimation("yourTactics", [0,1,2], 1.5);
			theCurrentAnimation.addAnimation("yourTools", [0,1,2,3], 1.5);
			theCurrentAnimation.addAnimation("yourTransform", [0,1,2,3,4,5], 1.5);
			theCurrentAnimation.addAnimation("yourCombo", [0,1,2,3,4,5,6], 1.5);
			theCurrentAnimation.addAnimation("yourChickens", [0,1,2,3], 1.5);
			theCurrentAnimation.addAnimationCallback(superCallback);
			mouseClicked = false;
			
			tutorialState = NONE;
		}
		
		public function overlapsPoint(x:Number, y:Number):Boolean {
			return backdrop.overlapsPoint(x, y) || theCurrentAnimation.overlapsPoint(x, y);
		}
		
		/**
		 * Returns true if this level does have a rule tutorial.
		 */
		public static function containsTutorialForLevel(level:int):Boolean {
			return level == 1 || level == 2 || level == 3 || level == 5 || level == 12;
		}
		
		public function playAnimation():void {
			if (level == 1) {
				add(backdrop);
				add(theCurrentAnimation);
				theCurrentAnimation.loadGraphic(Res.you, true, false, 506,506,false);
				theCurrentAnimation.play("you");
				tutorialState = YOU;
			} else if (level == 2) {
				add(backdrop);
				add(theCurrentAnimation);
				theCurrentAnimation.loadGraphic(Res.yourTactics, true, false, 506,506,false);
				theCurrentAnimation.play("yourTactics");
				tutorialState = YOURTACTICS;
			} else if (level == 3) {
				add(backdrop);
				add(theCurrentAnimation);
				theCurrentAnimation.loadGraphic(Res.yourTransform, true, false, 506,506,false);
				theCurrentAnimation.play("yourTransform");
				tutorialState = YOURTRANSFORM;
			} else if (level == 4) {
				add(backdrop);
				add(theCurrentAnimation);
				theCurrentAnimation.loadGraphic(Res.yourCombo, true, false, 506,506,false);
				theCurrentAnimation.play("yourCombo");
				tutorialState = YOURCOMBO;
			} else if (level == 5) {
				add(backdrop);
				add(theCurrentAnimation);
				theCurrentAnimation.loadGraphic(Res.yourChickens, true, false, 506,506,false);
				theCurrentAnimation.play("yourChickens");
				tutorialState = YOURCHICKENS;
			}
			
		}
		
		public override function update():void {
			super.update();
			if (FlxG.mouse.justPressed()) mouseClicked = true;
		}
		
		
		
		private function superCallback(name:String, frameNumber:int, frameIndex:int):void {
			if (tutorialState == YOU) {
				if (mouseClicked) {
					mouseClicked = false;
					theCurrentAnimation.loadGraphic(Res.yourTask, true, false, 506,506,false);
					theCurrentAnimation.play("yourTask");
					
					tutorialState = YOURTASK;
				}
				
			} else if (tutorialState == YOURTASK) {
				if (mouseClicked) {
					mouseClicked = false;
					theCurrentAnimation.loadGraphic(Res.yourTools, true, false, 506,506,false);
					theCurrentAnimation.play("yourTools");
					
					tutorialState = YOURTOOLS;
				}
			} else {
				if (mouseClicked) {
					mouseClicked = false;
					theCurrentAnimation.loadGraphic(Res.blankImage, false, false, 1,1,false);
					theCurrentAnimation.play("silent");
					remove(theCurrentAnimation);
					remove(backdrop);
					
					tutorialState = NONE;
					isOver = true;
				}
			}
		}
		
		public function over():Boolean {
			return isOver;
		}
	}
}